package com.xxkk.mvc.service;

import com.xxkk.gameCore.net.sql.mapper.UserInfoMapper;
import com.xxkk.gameCore.net.sql.pojo.UserInfo;
import com.xxkk.gameCore.net.sql.pojo.UserInfoExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SqlService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    public UserInfo getUserInfoById(int id) {
        UserInfoExample example = new UserInfoExample();
        example.createCriteria().andIdEqualTo(id);
        List<UserInfo> list = userInfoMapper.selectByExample(example);
        return list.isEmpty() ? null : list.get(0);
    }

    public UserInfo getUserInfoByAccount(String account) {
        UserInfoExample example = new UserInfoExample();
        example.createCriteria().andAccountEqualTo(account);
        List<UserInfo> list = this.userInfoMapper.selectByExample(example);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    public boolean updateUserInfo(UserInfo userInfo) {
        UserInfoExample example = new UserInfoExample();
        example.createCriteria().andIdEqualTo(userInfo.getId())
                .andPlatformIdEqualTo(userInfo.getPlatformId())
                .andAccountEqualTo(userInfo.getAccount())
                .andPasswordEqualTo(userInfo.getPassword());
        int res = this.userInfoMapper.updateByExample(userInfo, example);
        return res > 0;
    }

    public UserInfo getUserInfoByAPP(String account, String pwd, String platformId) {
        UserInfoExample example = new UserInfoExample();
        example.createCriteria().andPlatformIdEqualTo(platformId)
                .andAccountEqualTo(account)
                .andPasswordEqualTo(pwd);
        List<UserInfo> list = userInfoMapper.selectByExample(example);
        return list.isEmpty() ? null : list.get(0);
    }

    /**
     * 获取用户信息
     * @param account 账号
     * @param pwd，密码
     * @return
     */
    public UserInfo getUserInfoByAP(String account, String pwd) {
        UserInfoExample example = new UserInfoExample();
        example.createCriteria()
                .andAccountEqualTo(account)
                .andPasswordEqualTo(pwd);
        List<UserInfo> list = userInfoMapper.selectByExample(example);
        return list.isEmpty() ? null : list.get(0);
    }

}
